
<?php
//Name: register.php
//Purpose: This script is for registering a new user with the application
//CreateDate: 10/01/12
if(isset($_POST['submit1']))
{
	
	include 'util.php';
	
	$sanitized_username = filter_var($_POST['username'], FILTER_SANITIZE_STRING);
	
	$usernameExists = usernameExists($sanitized_username);
	
	if($usernameExists)
	{
		$loginSucess = false;

	}
	else 
	{
	$filename = "users.txt";
	$file = fopen( $filename, "a" );
	if( $file == false )
	{
	   echo ( "Error in opening new file" );
	   exit();
	}
	
	fwrite($file, "\n");
		
	///USERNAME
	if(isset($_POST['username']))
	{
		$sanitized_username = filter_var($_POST['username'], FILTER_SANITIZE_STRING);
		fwrite($file, $sanitized_username.'|');
	}
	else
	{
		//if username is not posted
	}
	
	
	///PASSWORD
	if(isset($_POST['password']))
	{
		$password = $_POST['password']; 
		fwrite($file, $_POST['password'].'|');
	}
	
	
	///FIRST NAME
	if(isset($_POST['firstName']))
	{
		$sanitized_firstName = filter_var($_POST['firstName'], FILTER_SANITIZE_STRING);
		fwrite($file, $sanitized_firstName.'|');
	}
	else
	{
		fwrite($file, '|');
	}
	
	
	///LAST NAME
	if(isset($_POST['lastName']))
	{
		$sanitized_lastName = filter_var($_POST['lastName'], FILTER_SANITIZE_STRING);
		fwrite($file, $sanitized_lastName.'|');
	}
	else
	{
		fwrite($file, '|');
	}
	
	
	///DOB
	if(isset($_POST['dob']))
	{
		fwrite($file, $_POST['dob'].'|');
	}
	else
	{
		//dob register
	}
	
	
	
	if(isset($_POST['gender']))
	{
		$sanitized_gender = filter_var($_POST['gender'], FILTER_SANITIZE_STRING);
		fwrite($file, $sanitized_gender.'|');
	}
	else
	{
		// if gender is not set
	}
	
	
	
	//make sure the post exists
	if(isset($_POST['email']))
	{
		$email = $_POST['email'];
		
		$sanitized_email = filter_var($email, FILTER_SANITIZE_EMAIL);
		
		//make sure valid email
		if(filter_var($sanitized_email, FILTER_VALIDATE_EMAIL))
		{
			fwrite($file, $sanitized_email.'|');
		}
		else
		{
			//to notify user of bad email.
			
		}
	}
	else
	{
		//if email is not posted
	}
	
	fclose( $file );
	
	$loginSucess = loginUser($sanitized_username, $password);
	
	if($loginSucess != null && $loginSucess != "")
	{
		session_name('login');
		session_start();
		$_SESSION['username'] = $loginSucess;
		session_write_close();
		header("location: /home.php");
		//change the source of the header iframe
	}
	}
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
	<script type="text/javascript" src="javascript/tcaparrel.js">
	</script>
	<link rel="StyleSheet" href="css/header.css" type="text/css">
	<LINK REL=StyleSheet HREF="css/tcaparrel.css" TYPE="text/css">
</head>

<body class="registerPage">
<div class="contentWrapper">
	<div class="header">
		<?php 
		session_name('login');
		session_start();
		if(isset($_SESSION['username']))
		{
			include('loggedInUserHeader.php');
		}
		else
		{
			include ('loginformheader.php');
		}
		?>
	</div>
	
	<div class="menubarContentWrapper">
			<div class="menubar">
				<div class="menuItem" onclick="sweatshirts_click()"><label style="cursor: inherit">SweatShirts</label></div>
				<div class="menuItem" onclick="hats_click()"><label style="cursor: inherit">Hats</label></div>
				<div class="menuItem" onclick="tshirts_click()"><label style="cursor: inherit">T-Shirts</label></div>
			</div>
	</div>
	<?php
	session_name('login');
	 session_start();
	 
	if(isset($_SESSION['loginStatus']))
	{
		if($_SESSION['loginStatus'] == "fail")
		{
			echo '<p>Login Failed!.. Register Below</p>';
			$_SESSION['loginStatus'] = "";
			session_write_close();
		}	
	}
		
	?>
	<h1>Register</h1>
	
	<form name="registerForm" action="register.php" method="post" onsubmit="return validateForm()">
		<table>
			<tr>
				<td>
					<label>
						First Name: 
					</label>
				</td>
				<td>
					<input type="text" name="firstName" maxlength="20" />
				</td>
			</tr>
			<tr>
				<td>
					<label>
						Last Name:
					</label>
				</td>
				<td>
					<input type="text" name="lastName" maxlength="30" />
				</td>
			</tr>
			<tr>
				<td>
					<label>
						Date of Birth*:
					</label>
				</td>
				<td>
					<select id="month" name="month" onchange="dateSelect()">
						<option value="01">January</option>
						<option value="02">Feburary</option>
						<option value="03">March</option>
						<option value="04">April</option>
						<option value="05">May</option>
						<option value="06">June</option>
						<option value="07">July</option>
						<option value="08">August</option>
						<option value="09">September</option>
						<option value="10">October</option>
						<option value="11">November</option>
						<option value="12">December</option>					
					</select>
					<select id="day" name="day" onchange="dateSelect()">
						<option value="01">1</option>
						<option value="02">2</option>
						<option value="03">3</option>
						<option value="04">4</option>
						<option value="05">5</option>
						<option value="06">6</option>
						<option value="07">7</option>
						<option value="08">8</option>
						<option value="09">9</option>
						<option value="10">10</option>
						<option value="11">11</option>
						<option value="12">12</option>
						<option value="13">13</option>
						<option value="14">14</option>
						<option value="15">15</option>
						<option value="16">16</option>
						<option value="17">17</option>
						<option value="18">18</option>
						<option value="19">19</option>
						<option value="20">20</option>
						<option value="21">21</option>
						<option value="22">22</option>
						<option value="23">23</option>
						<option value="24">24</option>
						<option value="25">25</option>
						<option value="26">26</option>
						<option value="27">27</option>
						<option value="28">28</option>
						<option value="29">29</option>
						<option value="30">30</option>
						<option value="31">31</option>
					</select>
					<select id="year" name="year" onchange="dateSelect()">
						<option value="2000">2000</option>
						<option value="1999">1999</option>
						<option value="1998">1998</option>
						<option value="1997">1997</option>
						<option value="1996">1996</option>
						<option value="1995">1995</option>
						<option value="1994">1994</option>
						<option value="1993">1993</option>
						<option value="1992">1992</option>
						<option value="1991">1991</option>
						<option value="1990">1990</option>
						<option value="1989">1989</option>
						<option value="1988">1988</option>
						<option value="1987">1987</option>
						<option value="1986">1986</option>
						<option value="1985">1985</option>
						<option value="1984">1984</option>
						<option value="1983">1983</option>
						<option value="1982">1982</option>
						<option value="1981">1981</option>
						<option value="1980">1980</option>
						<option value="1979">1979</option>
						<option value="1978">1978</option>
						<option value="1977">1977</option>
						<option value="1976">1976</option>
						<option value="1975">1975</option>
						<option value="1974">1974</option>
						<option value="1973">1973</option>
						<option value="1972">1972</option>
						<option value="1971">1971</option>
						<option value="1970">1970</option>
						<option value="1969">1969</option>
						<option value="1968">1968</option>
						<option value="1967">1967</option>
						<option value="1966">1966</option>
						<option value="1965">1965</option>
						<option value="1964">1964</option>
						<option value="1963">1963</option>
						<option value="1962">1962</option>
						<option value="1961">1961</option>
						<option value="1960">1960</option>
					</select>
				</td>
			</tr>
			<tr id="dobTableRow">
				<td>(mm/dd/yyyy)</td>
				<td>
					<input id="dob" type="text" name="dob"/>
				</td>
			</tr>
			<tr id="genderTableRow">
				<td>
					<label>Gender*:</label>
				</td>
				<td>
					<input type="radio" name="gender" value="male" /> Male
					<input type="radio" name="gender" value="female" /> Female
				</td>
			</tr>
			<tr id="emailTableRow">
				<td>
					<label>
						Email*: 
					</label><br/>
					<label>example@email.com</label>
				</td>
				<td>
					<input type="text" name="email" />
				</td>
			</tr >
			<tr id="usernameTableRow" >
				<td>
					<label>
						Username*:
					</label>
				</td>
				<td>
					<input type="text" name="username" maxlength="15" />
				</td>
				<?php
					
				if($usernameExists)
				{
					echo '<td class="validate">Username already Exists!</td>';
				}
				
				?>			
			</tr>
			<tr id="passwordTableRow">
				<td>
					<label>
						Password*:
					</label>
				</td>
				<td>
					<input type="password" name="password" maxlength="15" />
				</td>
			</tr>
			<tr>
				<td>
					<label>Favortie Schools:</label>
				</td>
				<td>
					<input type="checkbox" value="metroState"/>MetroState <br/>
					<input type="checkbox" value="hamline"/>Hamline<br/>
				</td>
			</tr>
			<tr>
				<td colspan="2" align="right">
					<input type="submit" name="submit1"  value="Register" />
				</td>
			</tr>
		</table>
	</form>
	</div>
</body>

</html>